package org.halk.schedule;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.halk.entity.StudentTest;
import org.halk.entity.UserTest;
import org.halk.repository.UserTestRepository;
import org.halk.service.StudentService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class SimpleXxlJob {

    @Resource
    private UserTestRepository userTestRepository;
    @Resource
    private StudentService studentService;


    @XxlJob("demoJobHandler")
    public void demoJobHandler(String param){
        // 获取任务上下文
        String jobParam = XxlJobHelper.getJobParam();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        XxlJobHelper.log("执行定时任务,执行时间:{}，接收到的参数:{}", simpleDateFormat.format(new Date()), jobParam);

        StudentTest studentTest = studentService.queryById(1);
        XxlJobHelper.log(studentTest.toString());

        double random = Math.random();
        if (random < 0.5) {
            int i = 1/0;
        }
    }

    public void test(){
        List<UserTest> all = userTestRepository.findAll();
        for (UserTest userTest : all) {
            log.info(userTest.toString());
        }

        StudentTest studentTest = studentService.queryById(1);
        log.info(studentTest.toString());
    }
}
